<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>spring汇总 | 曙光</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="Spring整体汇总">
<meta property="og:type" content="article">
<meta property="og:title" content="spring汇总">
<meta property="og:url" content="https://sonicbrother.gitee.io/2021/04/07/spring%E6%B1%87%E6%80%BB/index.html">
<meta property="og:site_name" content="曙光">
<meta property="og:description" content="Spring整体汇总">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-04-07T15:09:46.000Z">
<meta property="article:modified_time" content="2021-04-07T15:11:03.340Z">
<meta property="article:author" content="小蚂蚁">
<meta property="article:tag" content="spring">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="曙光" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">曙光</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS 订阅"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="搜索"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://sonicbrother.gitee.io"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-spring汇总" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2021/04/07/spring%E6%B1%87%E6%80%BB/" class="article-date">
  <time class="dt-published" datetime="2021-04-07T15:09:46.000Z" itemprop="datePublished">2021-04-07</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      spring汇总
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h1 id="Spring整体汇总"><a href="#Spring整体汇总" class="headerlink" title="Spring整体汇总"></a>Spring整体汇总</h1><span id="more"></span>

<h2 id="Spring生命周期"><a href="#Spring生命周期" class="headerlink" title="Spring生命周期"></a>Spring生命周期</h2><h3 id="一。Spring概述"><a href="#一。Spring概述" class="headerlink" title="一。Spring概述"></a>一。Spring概述</h3><p>工厂模式：解耦合</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">创建一切想要的对象</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BeanFactory</span></span>&#123;</span><br><span class="line">  </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> Object <span class="title">getBean</span><span class="params">(String key)</span></span>&#123;</span><br><span class="line">         Object ret = <span class="keyword">null</span>;</span><br><span class="line">         <span class="keyword">try</span> &#123;</span><br><span class="line">             Class clazz = Class.forName(env.getProperty(key));</span><br><span class="line">             ret = clazz.newInstance();</span><br><span class="line">         &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">         &#125;</span><br><span class="line">         <span class="keyword">return</span> ret;</span><br><span class="line">     &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">1. 定义类型 (类)</span><br><span class="line">2. 通过配置文件的配置告知工厂(applicationContext.properties)</span><br><span class="line">   key &#x3D; value</span><br><span class="line">3. 通过工厂获得类的对象</span><br><span class="line">   Object ret &#x3D; BeanFactory.getBean(&quot;key&quot;)</span><br></pre></td></tr></table></figure>

<h3 id="二。Spring程序"><a href="#二。Spring程序" class="headerlink" title="二。Spring程序"></a>二。Spring程序</h3><p>ApplicationContext:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">非web环境 ： ClassPathXmlApplicationContext (main junit)</span><br><span class="line">web环境  ：  XmlWebApplicationContext</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">getBean(&quot;person&quot;)</span><br><span class="line">getBean(&quot;person&quot;, Person.class)</span><br><span class="line">ctx.getBean(Person.class)</span><br><span class="line">getBeanDefinitionNames()</span><br><span class="line">ctx.getBeanNamesForType(Person.class)</span><br><span class="line">containsBeanDefinition(&quot;a&quot;)-&gt;name标签定义的别名不能查找</span><br><span class="line">containsBean(&quot;person&quot;)-&gt;name标签定义的别名也可以查找</span><br></pre></td></tr></table></figure>

<h3 id="四。注入：解耦合"><a href="#四。注入：解耦合" class="headerlink" title="四。注入：解耦合"></a>四。注入：解耦合</h3><p>注入：通过Spring的配置文件，为成员变量赋值</p>
<p>化简注入方式：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">JDK类型注入 </span><br><span class="line">&lt;property name&#x3D;&quot;name&quot; value&#x3D;&quot;suns&quot;&#x2F;&gt;</span><br><span class="line">注意：value属性 只能简化 8种基本类型+String 注入标签</span><br><span class="line"></span><br><span class="line">用户自定义类型</span><br><span class="line">&lt;property name&#x3D;&quot;userDAO&quot; ref&#x3D;&quot;userDAO&quot;&#x2F;&gt;</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">JDK类型注入 </span><br><span class="line">&lt;bean id&#x3D;&quot;person&quot; class&#x3D;&quot;xxx.Person&quot; p:name&#x3D;&quot;suns&quot;&#x2F;&gt;</span><br><span class="line">注意：value属性 只能简化 8种基本类型+String 注入标签</span><br><span class="line"></span><br><span class="line">用户自定义类型</span><br><span class="line">&lt;bean id&#x3D;&quot;userService&quot; class&#x3D;&quot;xxx.UserServiceImpl&quot; p:userDAO-ref&#x3D;&quot;userDAO&quot;&#x2F;&gt;</span><br></pre></td></tr></table></figure>

<h3 id="八。工厂创建复杂对象"><a href="#八。工厂创建复杂对象" class="headerlink" title="八。工厂创建复杂对象"></a>八。工厂创建复杂对象</h3><p>不能直接new的对象称为复杂对象</p>
<ul>
<li><p>实现FactoryBean接口</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">getBean(&quot;id&quot;)获取的为复杂对象，getBean(&quot;&amp;id&quot;)为id后的Class对象</span><br></pre></td></tr></table></figure></li>
<li><p>实例工厂：避免代码侵入，整合遗留系统</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;connFactory&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.baizhiedu.factorybean.ConnectionFactory&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">  </span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;conn&quot;</span>  <span class="attr">factory-bean</span>=<span class="string">&quot;connFactory&quot;</span> <span class="attr">factory-method</span>=<span class="string">&quot;getConnection&quot;</span>/&gt;</span></span><br></pre></td></tr></table></figure></li>
<li><p>静态工厂：避免代码侵入，整合遗留系统</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">xxxxxxxxxx <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;conn&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.baizhiedu.factorybean.StaticConnectionFactory&quot;</span> <span class="attr">factory-method</span>=<span class="string">&quot;getConnection&quot;</span>/&gt;</span></span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="十。对象的生命周期"><a href="#十。对象的生命周期" class="headerlink" title="十。对象的生命周期"></a>十。对象的生命周期</h3><h4 id="生命周期三个阶段："><a href="#生命周期三个阶段：" class="headerlink" title="生命周期三个阶段："></a>生命周期三个阶段：</h4><blockquote>
<p>创建对象（new）-》注入属性（di）-》再调用初始化方法</p>
</blockquote>
<p>​    创建阶段：单例：随工厂创建（加入lazy-init=true，则也是获取时候创建对象），多例：获取的时候创建</p>
<p>​    初始化阶段：InitializingBean-&gt;afterPropertiesSet/init-method=xx。初始化方法由程序员提供，由工厂进行调用。（两个执行顺序：先接口，后配置）</p>
<p>​    销毁阶段：DisposableBean-&gt;destory/destory-method。销毁方法只适用于singleton，多例对象由虚拟机负责</p>
<h3 id="十一。配置文件参数化"><a href="#十一。配置文件参数化" class="headerlink" title="十一。配置文件参数化"></a>十一。配置文件参数化</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">context:property-placeholder</span> <span class="attr">location</span>=<span class="string">&quot;xx.properties&quot;</span>/&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="十二。类型转换器"><a href="#十二。类型转换器" class="headerlink" title="十二。类型转换器"></a>十二。类型转换器</h3><h4 id="自定义类型转换器"><a href="#自定义类型转换器" class="headerlink" title="自定义类型转换器"></a>自定义类型转换器</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">1.实现Converter&lt;String, Date&gt;接口</span><br><span class="line">2.在配置文件中注册自定义类型转换器 <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;myDateConverter&quot;</span> </span></span><br><span class="line"><span class="tag"><span class="attr">3.</span>注册类型转换器</span></span><br><span class="line">&lt;!--用于注册类型转换器--&gt;</span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;conversionService&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.context.support.ConversionServiceFactoryBean&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;converters&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">set</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">ref</span> <span class="attr">bean</span>=<span class="string">&quot;myDateConverter&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">set</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">日期格式：2020&#x2F;05&#x2F;01 (不支持 ：2020-05-01)</span><br></pre></td></tr></table></figure>

<h3 id="十三。后置处理bean"><a href="#十三。后置处理bean" class="headerlink" title="十三。后置处理bean"></a>十三。后置处理bean</h3><p>BeanPostProcesser:AOP</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">创建对象（new）-&gt; 注入属性（DI）-&gt;postProcessBeforeInitialization(BeanPostProcesser)-&gt;afterPropertiesSet(InitializingBean) -&gt;init-method(xml)-&gt;postProcessAfterInitialization(BeanPostProcesser)-&gt;destory(DIsposableBean)-&gt;destory-method(xml)</span><br></pre></td></tr></table></figure>

<hr>
<h2 id="代理开发"><a href="#代理开发" class="headerlink" title="代理开发"></a>代理开发</h2><p>代理类=原始功能+额外功能+相同接口</p>
<h3 id="静态代理"><a href="#静态代理" class="headerlink" title="静态代理"></a>静态代理</h3><p>代码冗余，维护困难</p>
<h3 id="动态代理"><a href="#动态代理" class="headerlink" title="动态代理"></a>动态代理</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">原始对象</span><br><span class="line">&lt;bean</span><br><span class="line">额外功能</span><br><span class="line">&lt;bean</span><br><span class="line">切入点</span><br><span class="line"><span class="tag">&lt;<span class="name">aop:config</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">id</span> = <span class="string">&quot;pc&quot;</span> <span class="attr">expression</span>=<span class="string">&quot;execution(* *.*(..))&quot;</span></span></span><br><span class="line">&lt;/aop:config&gt;</span><br><span class="line">组装</span><br><span class="line">    <span class="tag">&lt;<span class="name">aop:advisor</span> <span class="attr">advice-ref</span>=<span class="string">&quot;before&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;pc&quot;</span></span></span><br></pre></td></tr></table></figure>

<p>动态代理是使用动态字节码技术，在内存中直接生成字节码</p>
<p>MethodIntercepter(方法拦截器)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// org.aopalliance.MethodInterceptor </span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Arround</span> <span class="keyword">implements</span> <span class="title">MethodInterceptor</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">invoke</span><span class="params">(MethodInvocation invocation)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line">          System.out.println(<span class="string">&quot;-----额外功能 log----&quot;</span>);</span><br><span class="line">          Object ret = invocation.proceed();</span><br><span class="line">          <span class="keyword">return</span> ret;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>切入点：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">*  *(..)  --&gt; 所有方法</span><br><span class="line"></span><br><span class="line">* ---&gt; 修饰符 返回值</span><br><span class="line">* ---&gt; 方法名</span><br><span class="line">()---&gt; 参数表</span><br><span class="line">..---&gt; 对于参数没有要求 (参数有没有，参数有几个都行，参数是什么类型的都行)</span><br><span class="line"></span><br><span class="line">execution</span><br><span class="line">within</span><br><span class="line">args</span><br><span class="line">@annotation</span><br></pre></td></tr></table></figure>

<h3 id="AOP编程"><a href="#AOP编程" class="headerlink" title="AOP编程"></a>AOP编程</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">1. 原始对象</span><br><span class="line">2. 额外功能 (MethodInterceptor)</span><br><span class="line">3. 切入点</span><br><span class="line">4. 组装切面 (额外功能+切入点)</span><br></pre></td></tr></table></figure>

<p>JDK的动态代理:Proxy.newProxyInstance</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestJDKProxy</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        1. 借用类加载器  TestJDKProxy</span></span><br><span class="line"><span class="comment">                       UserServiceImpl</span></span><br><span class="line"><span class="comment">        2. JDK8.x前 内部类访问外部变量需要使用final修饰</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">            final UserService userService = new UserServiceImpl();</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//1 创建原始对象</span></span><br><span class="line">        UserService userService = <span class="keyword">new</span> UserServiceImpl();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//2 JDK创建动态代理</span></span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line"></span><br><span class="line">        InvocationHandler handler = <span class="keyword">new</span> InvocationHandler()&#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> Object <span class="title">invoke</span><span class="params">(Object proxy, Method method, Object[] args)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;------proxy  log --------&quot;</span>);</span><br><span class="line">                <span class="comment">//原始方法运行</span></span><br><span class="line">                Object ret = method.invoke(userService, args);</span><br><span class="line">                <span class="keyword">return</span> ret;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line"></span><br><span class="line">        UserService userServiceProxy = (UserService)Proxy.newProxyInstance(UserServiceImpl.class.getClassLoader(),userService.getClass().getInterfaces(),handler);</span><br><span class="line"></span><br><span class="line">        userServiceProxy.login(<span class="string">&quot;suns&quot;</span>, <span class="string">&quot;123456&quot;</span>);</span><br><span class="line">        userServiceProxy.register(<span class="keyword">new</span> User());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>CGlib动态代理</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.baizhiedu.cglib;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.baizhiedu.proxy.User;</span><br><span class="line"><span class="keyword">import</span> org.springframework.cglib.proxy.Enhancer;</span><br><span class="line"><span class="keyword">import</span> org.springframework.cglib.proxy.MethodInterceptor;</span><br><span class="line"><span class="keyword">import</span> org.springframework.cglib.proxy.MethodProxy;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Method;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestCglib</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//1 创建原始对象</span></span><br><span class="line">        UserService userService = <span class="keyword">new</span> UserService();</span><br><span class="line"></span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">          2 通过cglib方式创建动态代理对象</span></span><br><span class="line"><span class="comment">            Proxy.newProxyInstance(classloader,interface,invocationhandler)</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">            Enhancer.setClassLoader()</span></span><br><span class="line"><span class="comment">            Enhancer.setSuperClass()</span></span><br><span class="line"><span class="comment">            Enhancer.setCallback();  ---&gt; MethodInterceptor(cglib)</span></span><br><span class="line"><span class="comment">            Enhancer.create() ---&gt; 代理</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line"></span><br><span class="line">        Enhancer enhancer = <span class="keyword">new</span> Enhancer();</span><br><span class="line"></span><br><span class="line">        enhancer.setClassLoader(TestCglib.class.getClassLoader());</span><br><span class="line">        enhancer.setSuperclass(userService.getClass());</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        MethodInterceptor interceptor = <span class="keyword">new</span> MethodInterceptor() &#123;</span><br><span class="line">            <span class="comment">//等同于 InvocationHandler --- invoke</span></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> Object <span class="title">intercept</span><span class="params">(Object o, Method method, Object[] args, MethodProxy methodProxy)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line">                System.out.println(<span class="string">&quot;---cglib log----&quot;</span>);</span><br><span class="line">                Object ret = method.invoke(userService, args);</span><br><span class="line"></span><br><span class="line">                <span class="keyword">return</span> ret;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;;</span><br><span class="line"></span><br><span class="line">        enhancer.setCallback(interceptor);</span><br><span class="line"></span><br><span class="line">        UserService userServiceProxy = (UserService) enhancer.create();</span><br><span class="line"></span><br><span class="line">        userServiceProxy.login(<span class="string">&quot;suns&quot;</span>, <span class="string">&quot;123345&quot;</span>);</span><br><span class="line">        userServiceProxy.register(<span class="keyword">new</span> User());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="基于注解AOP编程"><a href="#基于注解AOP编程" class="headerlink" title="基于注解AOP编程"></a>基于注解AOP编程</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">原始对象</span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> </span></span><br><span class="line"><span class="tag"></span></span><br><span class="line"><span class="tag">额外功能</span></span><br><span class="line"><span class="tag">@<span class="attr">Aspect</span></span></span><br><span class="line"><span class="tag"><span class="attr">public</span> <span class="attr">class</span> <span class="attr">MyAspect</span> &#123;</span></span><br><span class="line"><span class="tag">    @<span class="attr">Pointcut</span>(&quot;<span class="attr">execution</span>(* <span class="attr">login</span>(<span class="attr">..</span>))&quot;)</span></span><br><span class="line"><span class="tag">    <span class="attr">public</span> <span class="attr">void</span> <span class="attr">myPointcut</span>()&#123;&#125;</span></span><br><span class="line"><span class="tag"></span></span><br><span class="line"><span class="tag">    @<span class="attr">Around</span>(<span class="attr">value</span>=<span class="string">&quot;myPointcut()&quot;</span>)</span></span><br><span class="line"><span class="tag">    <span class="attr">public</span> <span class="attr">Object</span> <span class="attr">arround</span>(<span class="attr">ProceedingJoinPoint</span> <span class="attr">joinPoint</span>) <span class="attr">throws</span> <span class="attr">Throwable</span> &#123;</span></span><br><span class="line"><span class="tag">        <span class="attr">System.out.println</span>(&quot;<span class="attr">----aspect</span> <span class="attr">log</span> <span class="attr">------</span>&quot;);</span></span><br><span class="line"><span class="tag">        <span class="attr">Object</span> <span class="attr">ret</span> = <span class="string">joinPoint.proceed();</span></span></span><br><span class="line"><span class="tag">        <span class="attr">return</span> <span class="attr">ret</span>;</span></span><br><span class="line"><span class="tag">    &#125;</span></span><br><span class="line"><span class="tag">&#125;</span></span><br><span class="line"><span class="tag">      </span></span><br><span class="line"><span class="tag">  // 配置文件    </span></span><br><span class="line"> &lt;bean id=&quot;userService&quot; class=&quot;com.baizhiedu.aspect.UserServiceImpl&quot;/&gt;</span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">       切面</span></span><br><span class="line"><span class="comment">         1. 额外功能</span></span><br><span class="line"><span class="comment">         2. 切入点</span></span><br><span class="line"><span class="comment">         3. 组装切面</span></span><br><span class="line"><span class="comment">    --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;arround&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.baizhiedu.aspect.MyAspect&quot;</span>/&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!--告知Spring基于注解进行AOP编程--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">aop:aspectj-autoproxy</span> /&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="Spring整合MVC框架的核心思路"><a href="#Spring整合MVC框架的核心思路" class="headerlink" title="Spring整合MVC框架的核心思路"></a>Spring整合MVC框架的核心思路</h2><h6 id="1-准备工厂"><a href="#1-准备工厂" class="headerlink" title="1. 准备工厂"></a>1. 准备工厂</h6><figure class="highlight markdown"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="bullet">1.</span> Web开发过程中如何创建工厂</span><br><span class="line"><span class="code">      ApplicationContext ctx = new ClassPathXmlApplicationContext(&quot;/applicationContext.xml&quot;);</span></span><br><span class="line"><span class="code">                                   XmlWebApplicationContext()</span></span><br><span class="line"><span class="code">2. 如何保证工厂唯一同时被共用</span></span><br><span class="line"><span class="code">   被共用：Web request|session|ServletContext(application)</span></span><br><span class="line"><span class="code">   工厂存储在ServletContext这个作用域中 ServletContext.setAttribute(&quot;xxxx&quot;,ctx);</span></span><br><span class="line"><span class="code">   </span></span><br><span class="line"><span class="code">   唯一：ServletContext对象 创建的同时 ---》 ApplicationContext ctx = new ClassPathXmlApplicationContext(&quot;/applicationContext.xml&quot;);</span></span><br><span class="line"><span class="code">       </span></span><br><span class="line"><span class="code">        ServletContextListener ---&gt; ApplicationContext ctx = new ClassPathXmlApplicationContext(&quot;/applicationContext.xml&quot;);</span></span><br><span class="line"><span class="code">        ServletContextListener 在ServletContext对象创建的同时，被调用(只会被调用一次) ，把工厂创建的代码，写在ServletContextListener中，也会保证只调用一次，最终工厂就保证了唯一性</span></span><br><span class="line"><span class="code"> 3. 总结</span></span><br><span class="line"><span class="code">      ServletContextListener(唯一)</span></span><br><span class="line"><span class="code">             ApplicationContext ctx = new ClassPathXmlApplicationContext(&quot;/applicationContext.xml&quot;); // 唯一</span></span><br><span class="line"><span class="code">             ServletContext.setAttribute(&quot;xxx&quot;,ctx) //(共用)</span></span><br><span class="line"><span class="code">             </span></span><br><span class="line"><span class="code"> 4. Spring封装了一个ContextLoaderListener </span></span><br><span class="line"><span class="code">     1. 创建工厂</span></span><br><span class="line"><span class="code">     2. 把工厂存在ServletContext中</span></span><br></pre></td></tr></table></figure>

<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">ContextLoaderListener使用方式 </span><br><span class="line"></span><br><span class="line">web.xml</span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">listener</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">listener-class</span>&gt;</span>org.springframework.web.context.ContextLoaderListener<span class="tag">&lt;/<span class="name">listener-class</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">listener</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">context-param</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">param-name</span>&gt;</span>contextConfigLocation<span class="tag">&lt;/<span class="name">param-name</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">param-value</span>&gt;</span>classpath:applicationContext.xml<span class="tag">&lt;/<span class="name">param-value</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">context-param</span>&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="持久层整合"><a href="#持久层整合" class="headerlink" title="持久层整合"></a>持久层整合</h2><p>Mybatis开发步骤</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">map配置繁琐  代码冗余 </span><br><span class="line"></span><br><span class="line">1. 实体</span><br><span class="line">2. 实体别名         配置繁琐 </span><br><span class="line">3. 表</span><br><span class="line">4. 创建DAO接口</span><br><span class="line">5. 实现Mapper文件</span><br><span class="line">6. 注册Mapper文件   配置繁琐 </span><br><span class="line">7. MybatisAPI调用  代码冗余 </span><br></pre></td></tr></table></figure>

<p>整合思路</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--连接池--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;dataSource&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.alibaba.druid.pool.DruidDataSource&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;driverClassName&quot;</span> <span class="attr">value</span>=<span class="string">&quot;com.mysql.jdbc.Driver&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;url&quot;</span> <span class="attr">value</span>=<span class="string">&quot;jdbc:mysql://localhost:3306/suns?useSSL=false&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;username&quot;</span> <span class="attr">value</span>=<span class="string">&quot;root&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span> <span class="attr">value</span>=<span class="string">&quot;123456&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!--创建SqlSessionFactory SqlSessionFactoryBean--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;sqlSessionFactoryBean&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.mybatis.spring.SqlSessionFactoryBean&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;dataSource&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;dataSource&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;typeAliasesPackage&quot;</span> <span class="attr">value</span>=<span class="string">&quot;com.baizhiedu.entity&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;mapperLocations&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">list</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">value</span>&gt;</span>classpath:com.baizhiedu.mapper/*Mapper.xml<span class="tag">&lt;/<span class="name">value</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">list</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!--创建DAO对象 MapperScannerConfigure--&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;scanner&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.mybatis.spring.mapper.MapperScannerConfigurer&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;sqlSessionFactoryBeanName&quot;</span> <span class="attr">value</span>=<span class="string">&quot;sqlSessionFactoryBean&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;basePackage&quot;</span> <span class="attr">value</span>=<span class="string">&quot;com.baizhiedu.dao&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="Spring控制事务"><a href="#Spring控制事务" class="headerlink" title="Spring控制事务"></a>Spring控制事务</h3><p>事务传播属性</p>
<table>
<thead>
<tr>
<th>传播属性的值</th>
<th>外部不存在事务</th>
<th>外部存在事务</th>
<th>用法</th>
<th>备注</th>
</tr>
</thead>
<tbody><tr>
<td>REQUIRED</td>
<td>开启新的事务</td>
<td>融合到外部事务中</td>
<td>@Transactional(propagation = Propagation.REQUIRED)</td>
<td>增删改方法</td>
</tr>
<tr>
<td>SUPPORTS</td>
<td>不开启事务</td>
<td>融合到外部事务中</td>
<td>@Transactional(propagation = Propagation.SUPPORTS)</td>
<td>查询方法</td>
</tr>
<tr>
<td>REQUIRES_NEW</td>
<td>开启新的事务</td>
<td>挂起外部事务，创建新的事务</td>
<td>@Transactional(propagation = Propagation.REQUIRES_NEW)</td>
<td>日志记录方法中</td>
</tr>
<tr>
<td>NOT_SUPPORTED</td>
<td>不开启事务</td>
<td>挂起外部事务</td>
<td>@Transactional(propagation = Propagation.NOT_SUPPORTED)</td>
<td>极其不常用</td>
</tr>
<tr>
<td>NEVER</td>
<td>不开启事务</td>
<td>抛出异常</td>
<td>@Transactional(propagation = Propagation.NEVER)</td>
<td>极其不常用</td>
</tr>
<tr>
<td>MANDATORY</td>
<td>抛出异常</td>
<td>融合到外部事务中</td>
<td>@Transactional(propagation = Propagation.MANDATORY)</td>
<td>极其不常用</td>
</tr>
</tbody></table>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">1. 隔离属性   默认值 </span><br><span class="line">2. 传播属性   Required(默认值) 增删改   Supports 查询操作</span><br><span class="line">3. 只读属性   readOnly false  增删改   true 查询操作</span><br><span class="line">4. 超时属性   默认值 -1</span><br><span class="line">5. 异常属性   默认值 </span><br><span class="line"></span><br><span class="line">增删改操作   @Transactional</span><br><span class="line">查询操作     @Transactional(propagation&#x3D;Propagation.SUPPORTS,readOnly&#x3D;true)</span><br></pre></td></tr></table></figure>

<p>基于注解/标签的事务配置</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">基于注解 @Transaction的事务配置回顾</span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;userService&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.baizhiedu.service.UserServiceImpl&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;userDAO&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;userDAO&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!--DataSourceTransactionManager--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;dataSourceTransactionManager&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;dataSource&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;dataSource&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line">@Transactional(isolation=,propagation=,...)</span><br><span class="line">public class UserServiceImpl implements UserService &#123;</span><br><span class="line">    private UserDAO userDAO;</span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">tx:annotation-driven</span> <span class="attr">transaction-manager</span>=<span class="string">&quot;dataSourceTransactionManager&quot;</span>/&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- ========================================================--&gt;</span></span><br><span class="line">基于标签的事务配置</span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;userService&quot;</span> <span class="attr">class</span>=<span class="string">&quot;com.baizhiedu.service.UserServiceImpl&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;userDAO&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;userDAO&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!--DataSourceTransactionManager--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">&quot;dataSourceTransactionManager&quot;</span> <span class="attr">class</span>=<span class="string">&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;dataSource&quot;</span> <span class="attr">ref</span>=<span class="string">&quot;dataSource&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line">事务属性 </span><br><span class="line"><span class="tag">&lt;<span class="name">tx:advice</span> <span class="attr">id</span>=<span class="string">&quot;txAdvice&quot;</span> <span class="attr">transaction-manager</span>=<span class="string">&quot;dataSourceTransactionManager&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">tx:attributes</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;register&quot;</span> <span class="attr">isoloation</span>=<span class="string">&quot;&quot;</span>,<span class="attr">propagation</span>=<span class="string">&quot;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">tx:method</span>&gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">tx:method</span> <span class="attr">name</span>=<span class="string">&quot;login&quot;</span> <span class="attr">.....</span>&gt;</span><span class="tag">&lt;/<span class="name">tx:method</span>&gt;</span></span><br><span class="line">          <span class="comment">&lt;!--等效于 </span></span><br><span class="line"><span class="comment">          @Transactional(isolation=,propagation=,)</span></span><br><span class="line"><span class="comment">          public void register()&#123;&#125;--&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">tx:attributes</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">tx:advice</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">aop:config</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">aop:pointcut</span> <span class="attr">id</span>=<span class="string">&quot;pc&quot;</span> <span class="attr">expression</span>=<span class="string">&quot;execution(* com.baizhiedu.service.UserServiceImpl.register(..))&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">aop:pointcut</span>&gt;</span></span><br><span class="line">     <span class="tag">&lt;<span class="name">aop:advisor</span> <span class="attr">advice-ref</span>=<span class="string">&quot;txAdvice&quot;</span> <span class="attr">pointcut-ref</span>=<span class="string">&quot;pc&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">aop:advisor</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">aop:config</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="注解编程"><a href="#注解编程" class="headerlink" title="注解编程"></a>注解编程</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Scope</span>注解</span><br><span class="line"><span class="meta">@Lazy</span>注解</span><br><span class="line"><span class="meta">@PostConstruct</span> <span class="comment">// 生命周期相关</span></span><br><span class="line"><span class="meta">@PreDestroy</span> <span class="comment">// 生命周期相关</span></span><br></pre></td></tr></table></figure>

<p>配置文件相关</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&lt;context:property-placeholder location&#x3D;&quot;&quot;&#x2F;&gt;</span><br><span class="line">@PropertySource</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;context:component-scan base-package&#x3D;&quot;com.baizhiedu&quot;&#x2F;&gt; &#x2F;&#x2F; 注解扫描</span><br></pre></td></tr></table></figure>

<h3 id="Spring与yml"><a href="#Spring与yml" class="headerlink" title="Spring与yml"></a>Spring与yml</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">1. 集合处理的问题</span><br><span class="line">   SpringEL表达式解决</span><br><span class="line">   @Value(&quot;#&#123;&#39;$&#123;list&#125;&#39;.split(&#39;,&#39;)&#125;&quot;)</span><br><span class="line">2. 对象类型的YAML进行配置时 过于繁琐 </span><br><span class="line">   @Value(&quot;$&#123;account.name&#125;&quot;)</span><br><span class="line">   </span><br><span class="line">SpringBoot  @ConfigurationProperties</span><br></pre></td></tr></table></figure>


      
    </div>
    <footer class="article-footer">
      <a data-url="https://sonicbrother.gitee.io/2021/04/07/spring%E6%B1%87%E6%80%BB/" data-id="ckn7l9wdl0003xg7d97q05vz5" data-title="spring汇总" class="article-share-link">分享</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/spring/" rel="tag">spring</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
  
    <a href="/2021/04/05/Hashmap/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">后一篇</strong>
      <div class="article-nav-title">Hashmap</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/tags/Hashmap/" rel="tag">Hashmap</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/" rel="tag">linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spring/" rel="tag">spring</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">标签云</h3>
    <div class="widget tagcloud">
      <a href="/tags/Hashmap/" style="font-size: 10px;">Hashmap</a> <a href="/tags/linux/" style="font-size: 10px;">linux</a> <a href="/tags/spring/" style="font-size: 10px;">spring</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/04/">四月 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/04/07/spring%E6%B1%87%E6%80%BB/">spring汇总</a>
          </li>
        
          <li>
            <a href="/2021/04/05/Hashmap/">Hashmap</a>
          </li>
        
          <li>
            <a href="/2021/04/04/linux/">linux常用命令</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 小蚂蚁<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.4.1.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>